package com.xzy.sms.web.utils.velocity;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.tools.config.DefaultKey;

import com.google.common.collect.Lists;
import com.xzy.sms.web.system.PermissionConstants;

/**
 * 菜单工具栏
 * @author macong
 * @since 1.0
 * @version 2014-04-10 macong
 */
@DefaultKey("menu")
public class MenuTool {
    
    /** 管理页面菜单 */
    private static List<FirstLeverMenu> ADMIN_MENUS;
    
    /*** 初始化管理页面菜单 ***/
    static {
        ADMIN_MENUS = Lists.newArrayList();
        FirstLeverMenu fstMenu;
        
        // 首页
        fstMenu = new FirstLeverMenu("home", "首页", "/", null);
        ADMIN_MENUS.add(fstMenu);
        
        // 系统配置
        fstMenu = new FirstLeverMenu("adapter", "发送通道管理", "/admin/channelconfig", null, PermissionConstants.ADMIN);
        ADMIN_MENUS.add(fstMenu);
        
        // 系统配置
        fstMenu = new FirstLeverMenu("user_manager", "用户管理", "/admin/user", null, PermissionConstants.ADMIN);
        ADMIN_MENUS.add(fstMenu);
        
        // 系统配置
        fstMenu = new FirstLeverMenu("apps", "应用管理", "/application", null, PermissionConstants.USER);
        ADMIN_MENUS.add(fstMenu);
        
        // 系统配置
        fstMenu = new FirstLeverMenu("apps", "模板管理", "/template", null, PermissionConstants.USER);
        ADMIN_MENUS.add(fstMenu);
        
        // 系统配置
        fstMenu = new FirstLeverMenu("send_history", "发送记录", "/sms/list", null, PermissionConstants.USER);
        ADMIN_MENUS.add(fstMenu);
        
        // 系统配置
        fstMenu = new FirstLeverMenu("send_test", "发送测试", "/sms/sendtest", null, PermissionConstants.USER);
        ADMIN_MENUS.add(fstMenu);
        
        // 系统配置
        fstMenu = new FirstLeverMenu("send_exception", "异常记录", "/sms/exception", null, PermissionConstants.ADMIN);
        ADMIN_MENUS.add(fstMenu);
    }
    
    /**
     * 获取管理页面菜单
     * 
     * @return val
     */
    public static List<FirstLeverMenu> getAdminMenu() {
        List<FirstLeverMenu> newList = Lists.newArrayList();
        ShiroTool shiroTool = new ShiroTool();
        for (int i = 0; i < ADMIN_MENUS.size(); i++) {
            FirstLeverMenu fm = ADMIN_MENUS.get(i);
            // 防止静态变量被修改
            FirstLeverMenu newFirstLeverMenu = fm.copy();
            if (newFirstLeverMenu.getMenus() != null && newFirstLeverMenu.getMenus().size() > 0) {
                List<ActivatorMenu> lstAM = newFirstLeverMenu.getMenus();
                List<ActivatorMenu> lstNewAM = Lists.newArrayList();
                for (int j = 0; j < lstAM.size(); j++) {
                    if (shiroTool.hasPermission(lstAM.get(j).getPermStr())) {
                        lstNewAM.add(lstAM.get(j));
                    }
                }
                if (lstNewAM.size() > 0) {
                    newFirstLeverMenu.setMenus(lstNewAM);
                    newList.add(newFirstLeverMenu);
                }
            } else {
                if (shiroTool.hasPermission(newFirstLeverMenu.getPermStr())) {
                    newList.add(newFirstLeverMenu);
                }
            }
            
        }
        return newList;
    }
    
    /**
     * 判断id是否在ids里面
     * 
     * @param id id
     * @param ids ids
     * @return 是否存在
     */
    public boolean isContain(String id, String ids) {
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(ids)) {
            return false;
        }
        return ids.indexOf("," + id) != -1;
    }
}
